package cn.iocoder.yudao.module.pplan.dal.mysql.producerecord;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pplan.controller.admin.producerecord.vo.ProduceRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.app.producerecord.vo.AppProduceRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.producerecord.ProduceRecordDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalTime;

/**
 * 生产计划-生产计划记录 Mapper
 *
 * @author yisl
 */
@Mapper
public interface ProduceRecordMapper extends BaseMapperX<ProduceRecordDO> {

    default PageResult<ProduceRecordDO> selectPage(ProduceRecordPageReqVO reqVO) {
        LambdaQueryWrapperX<ProduceRecordDO> wrapperX = new LambdaQueryWrapperX<ProduceRecordDO>()
                .eqIfPresent(ProduceRecordDO::getFlightNo, reqVO.getFlightNo())
                .eqIfPresent(ProduceRecordDO::getDepTime, reqVO.getDepTime())
                .eqIfPresent(ProduceRecordDO::getMarkStatus, reqVO.getMarkStatus())
                .orderByDesc(ProduceRecordDO::getId);
        if (ObjectUtil.isNotEmpty(reqVO.getSearchType())){
            switch (reqVO.getSearchType()){
                case 1:
                    wrapperX.ge(ProduceRecordDO::getDepTime, LocalTime.parse("00:00"));
                    wrapperX.le(ProduceRecordDO::getDepTime, LocalTime.parse("06:00"));
                    break;
                case 2:
                    wrapperX.ge(ProduceRecordDO::getDepTime, LocalTime.parse("06:00"));
                    wrapperX.le(ProduceRecordDO::getDepTime, LocalTime.parse("12:00"));
                    break;
                case 3:
                    wrapperX.ge(ProduceRecordDO::getDepTime, LocalTime.parse("12:00"));
                    wrapperX.le(ProduceRecordDO::getDepTime, LocalTime.parse("18:00"));
                    break;
                case 4:
                    wrapperX.ge(ProduceRecordDO::getDepTime, LocalTime.parse("18:00"));
                    wrapperX.le(ProduceRecordDO::getDepTime, LocalTime.parse("23:59:59"));
                    break;
            }
        }
        return selectPage(reqVO, wrapperX);
    }

    default PageResult<ProduceRecordDO> appSelectPage(AppProduceRecordPageReqVO reqVO) {

        LambdaQueryWrapperX<ProduceRecordDO> queryWrapperX = new LambdaQueryWrapperX<ProduceRecordDO>()
                .likeIfPresent(ProduceRecordDO::getFlightNo, reqVO.getFlightNo())
                .eqIfPresent(ProduceRecordDO::getStatus, reqVO.getStatus());
        switch (reqVO.getSort()) {
            case "1":
                queryWrapperX.orderByDesc(ProduceRecordDO::getDepTime);
                break;
            case "2":
                queryWrapperX.orderByAsc(ProduceRecordDO::getDepTime);
                break;
        }
        return selectPage(reqVO, queryWrapperX);
    }

}